<template>
	<view class="gb-content">
		<view class="center-box">
			<image :src="userInfo.avatarUrl"></image>
			<view  v-if="bindMobile == true" class="box-text-1">Hi，请绑定手机号~</view>
			<view  v-if="bindMobile == false" class="box-text-1">Hi，请登录~</view>
			
			<view v-if="bindMobile == true" class="box-text-2">为了更好的体验小程序请授权绑定手机号~</view>
			<view v-if="bindMobile == false" class="box-text-2">为保证小程序的正常运行请授权登录~</view>
			
			<button v-if="bindMobile == true"  size="default" shape="circle"  class="box-btn-1" type="warning" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">授权绑定手机号</button>
			<!-- <button  v-if="bindMobile == false" open-type="getUserInfo" @getuserinfo="getUserInfo" withCredentials="true" class="box-btn-1">微信授权登录</button> -->
			<button  v-if="bindMobile == false"  @click="getUserInfo" withCredentials="true" class="box-btn-1">微信授权登录</button>
			<navigator open-type="navigateBack" class="box-btn-2" >暂不授权</navigator>
			
		</view>
		
	</view>
</template>

<script>
	export default{
		data() {
			return {
				returnUrl:'',
				bindMobile:false,
				userInfo:{
					avatarUrl:'/static/public/images/headimgurl.jpg',
					name:''
				}
			}
		},
		onLoad(options) {
			this.app.getWxOauthOpenId(this,options);//默认只有初次打开时触发获取openid,退出登陆后后清理openid后，再访问登陆页重新获取
			this.returnUrl = options.returnUrl;
		},
		methods: {
			async getPhoneNumber (e) {
				//小程序手机授权登陆
				if (e.detail.errMsg == "getPhoneNumber:ok") {
				    this.$u.post('weixin/api.index/loginByMpPhone', {
						iv:e.detail.iv,
						encryptedData:e.detail.encryptedData,
						openid:this.app.getWxOpenId()
					}).then(res => {
						
						if(res.code == 1){
							
							this.loginGo();
						}
					})
				}
			},
			loginGo(){
				//登录成功后执行
				if (typeof(this.returnUrl) == 'undefined') { //未读取到页面信息
					console.log(45566644);
					uni.redirectTo({
					    url: '/pages/member/center/index'
					});
					return false;
				}
				uni.redirectTo({
					url:'/'+decodeURIComponent(this.returnUrl),
				});
			},
			async getUserInfo(){
				var that =this;
				
				wx.getUserProfile({
					desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
					success: (result) => {
						console.log(result);
						if(result.errMsg == "getUserProfile:ok"){
							that.userInfo = result.userInfo;
							console.log(that.userInfo)
							that.$u.post('weixin/api.index/do_login', {
								userInfo:that.userInfo,
								openid:that.app.getWxOpenId(),
								share_token : uni.getStorageSync('share_token')
							}).then(res => {
								if(res.code == 1){
									that.app.setAuthCode(res.token);
									if (res.is_bind == 0){
										that.app.showToast('请先授权绑定手机号.');
										that.bindMobile = true;
									}else{
										that.loginGo();
									}
								}
							})
						}
					},
					fail:() => {
						this.$api.msg('授权失败:'.result.errMsg);
					}
				})
				
			}
		}
	}
</script>

<style lang="scss">
	.gb-content{
		background-color: #fff !important;
	}
	.center-box{
		display: flex;
		flex-direction: column;
		align-items: center;
		position: fixed;
		width: 100%;
		height: 100%;
		top: 0;
		bottom: 0;
		image{
			width: 200upx;
			height: 200upx;
			margin-top: 143upx;
			border: 1px solid #e3e3e3;
			border-radius: 50%;
		}
		.box-text-1{
			margin-top: 66upx;
			font-family: AdobeHeitiStd-Regular;
			font-size: 40rpx;
			font-weight: bold;
			font-stretch: normal;
			color: #333333;
		}
		.box-text-2{
			margin-top: 29upx;
			font-size: 27upx;
			color: #333333;
			
		}
		.box-btn-1{
			margin-top: 81upx;
			font-size: 35upx;
			width: 450upx;
			height: 75upx;
			display: flex;
			justify-content: center;
			align-items: center;
			background-color: #ff5428;
			border-radius: 35upx;
			color: #fff;
		}
		
		.box-btn-2{
			margin-top: 50upx;
			font-size: 30upx;
			text-decoration: underline;
		}
		
	}

</style>
